// packageA/pages/profileEdit/index.js
import http from '../../../utils/http';
import api from '../../../utils/api';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholder:"",
    child_id: "",
    childInfo: {},
    child: [],
    user_id: "",
    showPopup: false,
    truename: "",
    dialogTitle: "",
    dialogvalue: "",
    key: "",
    fileList:[]
  },
  handleDelImg(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
        fileList: []
    })
},
  afterRead(event) {
    let _this = this
    const {
        file
    } = event.detail;
    wx.uploadFile({
        url: http.report_host + api.uploadFile, // 仅为示例，非真实的接口地址
        filePath: file.url,
        name: 'file',
        formData: {
            category: "avatar"
        },
        success(res) {
            // 上传完成需要更新 fileList
            let doc = JSON.parse(res.data)
            // 上传完成需要更新 fileList
            const {
                fileList = []
            } = _this.data;
            fileList.push({
                ...file,
                url: doc.data.file_id
            });
            _this.setData({
              fileList: fileList,
              key:"avatar",
              dialogvalue:doc.data.file_id
          });
          _this.onSubmitPopup()

        },
    });
},
  init() {
    const child = wx.getStorageSync('child')
    const childInfo = wx.getStorageSync('childInfo')
    const userInfo = wx.getStorageSync('userInfo')
    this.setData({
      child_id: childInfo.id ? childInfo.id : 0,
      childInfo: childInfo,
      child: child,
      user_id: userInfo.id,
      fileList:childInfo.avatar?[{url:http.report_host+'/getFile/'+childInfo.avatar,isImage: true,}]:[]
    })
  },
  changeProfile(e) {
    let name = e.currentTarget.dataset.name
    switch (name) {
      case "name":
        this.setData({
          dialogTitle: "修改姓名",
          placeholder:"请输入姓名",
          dialogvalue: this.data.childInfo.name,
          key: name
        })
        break;
      case "age":
        this.setData({
          dialogTitle: "修改年龄",
          placeholder:"请输入年龄（岁）",
          dialogvalue: this.data.childInfo.age,
          key: name
        })
        break;
      case "height":
        this.setData({
          dialogTitle: "修改身高",
          placeholder:"请输入身高（厘米）",
          dialogvalue: this.data.childInfo.height,
          key: name
        })
        break;
      case "weight":
        this.setData({
          dialogTitle: "修改体重",
          placeholder:"请输入体重（kg）",
          dialogvalue: this.data.childInfo.weight,
          key: name
        })
        break;
      default:
        break;
    }
    this.setData({
      showPopup: true
    })
  },
  /** 提交修改 */
  onSubmitPopup() {
    http.putHttp(api.updateProfileInfo + this.data.child_id, {
        [this.data.key]: this.data.dialogvalue,
        user_id: this.data.user_id
      }, (result) => {
        if (result.code == 0) {
          wx.showToast({
            title: '修改成功',
          })
          wx.setStorageSync('child', result.data)
          result.data.forEach(ele => {
            if (ele.id == this.data.childInfo.id) {
              wx.setStorageSync('childInfo', ele)
              this.init()
            }
          })
        }

      },
      (err) => {
        console.log(err);
      },
    )
  },
  onClosePopup() {
    this.setData({
      showPopup: false,
      dialogvalue: ""
    })
  },
  /** 跳转到父母是否近视页面 */
  changeFaMa(){
    wx.navigateTo({
      url: '/packageA/pages/changeFaMa/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})